<template>
  <el-dialog title="分配角色" :visible="showDialogVisible" width="40%" @close="btnCencel">
    <!-- el-checkbox-group选中的是 当前用户所拥有的角色  需要绑定 当前用户拥有的角色-->
    <el-checkbox-group v-model="rolesId">
      <!-- 选项 -->
      <el-checkbox v-for="(item) in list" :key="item.id" :label="item.id">
        {{ item.name }}
      </el-checkbox>
    </el-checkbox-group>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button type="primary" size="small" @click="btnOk">确定</el-button>
        <el-button size="small" @click="btnCencel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getRoles } from '@/api/setting'
import { getUserDetaiInfoById } from '@/api/user'
import { assignRoles } from '@/api/employees'
export default {
  name: '',
  components: {},
  props: {
    showDialogVisible: {
      type: Boolean,
      require: true,
      default: false
    },

    userId: {
      default: '',
      type: [Number, String],
      require: true
    }

  },
  data() {
    return {
      list: [], // 角色列表
      rolesId: [] // 分配的角色id

    }
  },
  created() {
    this.getRoles()
  },
  methods: {
    // 获取所有角色
    async getRoles() {
      const { rows } = await getRoles({ page: 1, pagesize: 20 })
      this.list = rows
    },
    // 获取用户的所有角色id
    async getUserDetaiInfoById(id) {
      const { roleIds } = await getUserDetaiInfoById(id)
      this.rolesId = roleIds
    },
    // 修改用户角色
    async btnOk() {
      await assignRoles({
        id: this.userId,
        roleIds: this.rolesId
      })
      this.$message({
        message: '修改成功!',
        type: 'success',
        showClose: false,
        duration: 1000
      })

      this.$emit('update:showDialogVisible', false)
    },
    // 关闭弹层事件
    btnCencel() {
      this.rolesId = this.$options.data().rolesId // 还原选择项
      this.$emit('update:showDialogVisible', false)
    }
  }
}
</script>

<style lang="less" scoped>

</style>
